package com.fr.data.condition;

import com.fr.base.ArrayUtils;
import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.list.IntList;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.report.script.Calculator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ListCondition extends AbstractCondition
{
  private List joinConditionList = new ArrayList();

  public int getJoinConditionCount()
  {
    return this.joinConditionList.size();
  }

  public void addCondition(Condition paramCondition)
  {
    addJoinCondition(new JoinCondition(0, paramCondition));
  }

  public void addJoinCondition(int paramInt, Condition paramCondition)
  {
    addJoinCondition(new JoinCondition(paramInt, paramCondition));
  }

  public void addJoinCondition(JoinCondition paramJoinCondition)
  {
    Condition localCondition = paramJoinCondition.getCondition();
    if (localCondition == null)
      return;
    if ((localCondition instanceof ListCondition) && (((ListCondition)localCondition).getJoinConditionCount() == 0))
      return;
    this.joinConditionList.add(paramJoinCondition);
  }

  public JoinCondition getJoinCondition(int paramInt)
  {
    if ((paramInt < 0) || (paramInt >= this.joinConditionList.size()))
      return null;
    return ((JoinCondition)this.joinConditionList.get(paramInt));
  }

  public void setJoinCondition(int paramInt, JoinCondition paramJoinCondition)
  {
    this.joinConditionList.set(paramInt, paramJoinCondition);
  }

  public void removeJoinCondition(int paramInt)
  {
    if ((paramInt < 0) || (paramInt >= this.joinConditionList.size()))
      return;
    this.joinConditionList.remove(paramInt);
  }

  public void clearJoinConditions()
  {
    this.joinConditionList.clear();
  }

  public String[] getDependence()
  {
    String[] arrayOfString = new String[0];
    Iterator localIterator = this.joinConditionList.iterator();
    while (localIterator.hasNext())
    {
      Condition localCondition = ((JoinCondition)localIterator.next()).getCondition();
      arrayOfString = (String[])ArrayUtils.addAll(arrayOfString, localCondition.getDependence());
    }
    return arrayOfString;
  }

  public boolean eval(Object paramObject, Calculator paramCalculator)
  {
    return inner_eval(paramObject, paramCalculator);
  }

  public int[] evalTableData(TableData paramTableData, int[] paramArrayOfInt, int paramInt, Calculator paramCalculator)
  {
    int[] arrayOfInt = new int[0];
    if (paramTableData == null)
      return arrayOfInt;
    int i = 0;
    try
    {
      i = paramTableData.getRowCount();
    }
    catch (TableDataException localTableDataException)
    {
      FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
    }
    if (paramArrayOfInt == null)
      arrayOfInt = IntList.range(i);
    else
      arrayOfInt = paramArrayOfInt;
    int j = getJoinConditionCount();
    for (int k = 0; k < j; ++k)
    {
      JoinCondition localJoinCondition = getJoinCondition(k);
      Condition localCondition = localJoinCondition.getCondition();
      if ((k == 0) || (localJoinCondition.getJoin() == 0))
        arrayOfInt = localCondition.evalTableData(paramTableData, arrayOfInt, paramInt, paramCalculator);
      else if (localJoinCondition.getJoin() == 1)
        arrayOfInt = IntList.unionArray(arrayOfInt, localCondition.evalTableData(paramTableData, paramArrayOfInt, paramInt, paramCalculator));
    }
    return arrayOfInt;
  }

  private boolean inner_eval(Object paramObject, Calculator paramCalculator)
  {
    int i = getJoinConditionCount();
    if (i <= 0)
      return true;
    boolean bool = false;
    for (int j = 0; j < i; ++j)
    {
      JoinCondition localJoinCondition = getJoinCondition(j);
      int k = localJoinCondition.getJoin();
      Condition localCondition = localJoinCondition.getCondition();
      if (j == 0)
        bool = localCondition.eval(paramObject, paramCalculator);
      else if (k == 0)
        bool = (bool) && (localCondition.eval(paramObject, paramCalculator));
      else
        bool = (bool) || (localCondition.eval(paramObject, paramCalculator));
    }
    return bool;
  }

  public String toString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("(");
    for (int i = 0; i < this.joinConditionList.size(); ++i)
    {
      JoinCondition localJoinCondition = (JoinCondition)this.joinConditionList.get(i);
      if (i != 0)
        if (localJoinCondition.getJoin() == 0)
          localStringBuffer.append(" and ");
        else
          localStringBuffer.append(" or ");
      if (localJoinCondition.getCondition() != null)
        localStringBuffer.append(localJoinCondition.getCondition().toString());
    }
    localStringBuffer.append(")");
    return localStringBuffer.toString();
  }

  public boolean equals(Object paramObject)
  {
    if ((paramObject == null) || (!(paramObject instanceof ListCondition)))
      return false;
    ListCondition localListCondition = (ListCondition)paramObject;
    if (localListCondition.getJoinConditionCount() != getJoinConditionCount())
      return false;
    for (int i = 0; i < getJoinConditionCount(); ++i)
      if (!(ComparatorUtils.equals(localListCondition.getJoinCondition(i), getJoinCondition(i))))
        return false;
    return true;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isAttr())
      clearJoinConditions();
    if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("JoinCondition")))
    {
      JoinCondition localJoinCondition = new JoinCondition();
      this.joinConditionList.add(localJoinCondition);
      paramXMLableReader.readXMLObject(localJoinCondition);
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    int i = getJoinConditionCount();
    for (int j = 0; j < i; ++j)
    {
      JoinCondition localJoinCondition = getJoinCondition(j);
      localJoinCondition.writeXML(paramXMLPrintWriter);
    }
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    ListCondition localListCondition = (ListCondition)super.clone();
    ArrayList localArrayList = new ArrayList();
    for (int i = 0; i < getJoinConditionCount(); ++i)
      localArrayList.add(getJoinCondition(i).clone());
    localListCondition.joinConditionList = localArrayList;
    return localListCondition;
  }

  public JSONObject createJSON()
    throws Exception
  {
    return null;
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws Exception
  {
    JSONArray localJSONArray = paramJSONObject.getJSONArray("commonconditions");
    int i = 0;
    int j = localJSONArray.length();
    while (i < j)
    {
      CommonCondition localCommonCondition = new CommonCondition();
      localCommonCondition.parseJSON(localJSONArray.getJSONObject(i));
      addCondition(localCommonCondition);
      ++i;
    }
  }
}